
package org.apache.ocean.util;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 *
 * @author jasonr
 */
public class OneShotLatch {
  private final Sync sync = new Sync();
  
  public OneShotLatch() {
  }
  
  public void signal() {
    sync.releaseShared(0);
  }
  
  public void await() throws InterruptedException {
    sync.acquireSharedInterruptibly(0);
  }
  
  private class Sync extends AbstractQueuedSynchronizer {
    protected int tryAcquireShared(int ignored) {
      return (getState() == 1) ? 1 : -1;
    }
    
    protected boolean tryReleaseShared(int ignored) {
      setState(1);
      return true;
    }
  }
}
